package com.jnu.HomeworkApplication;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.map.TextOptions;
import com.baidu.mapapi.model.LatLng;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

public class SellerFragment extends Fragment {

    private MapView mapView;

    public SellerFragment() {
        // Required empty public constructor
    }

    public static SellerFragment newInstance() {
        SellerFragment fragment = new SellerFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_seller, container, false);
        mapView = rootView.findViewById(R.id.baidu_map);

        BaiduMap baiduMap = mapView.getMap();

        LatLng centerPoint = new LatLng(22.2559,113.541112);
        /*
        注释
        MapStatus mMapStatus = new MapStatus.Builder().target(centerPoint).zoom(17).build();
        MapStatusUpdate mMapStatusUpdate = MapStatusUpdateFactory.newMapStatus(mMapStatus);
        baiduMap.setMapStatus(mMapStatusUpdate);

        OverlayOptions markerOption = new MarkerOptions().icon(bitmap).position(centerPoint);
        baiduMap.addOverlay(markerOption);
         */
        MapStatus mMapStatus = new MapStatus.Builder().target(centerPoint).zoom(17).build();
        MapStatusUpdate mMapStatusUpdate = MapStatusUpdateFactory.newMapStatus(mMapStatus);
        baiduMap.setMapStatus(mMapStatusUpdate);
        BitmapDescriptor bitmap = BitmapDescriptorFactory.fromResource(R.drawable.diana);

        ShopLoader shopLoader = new ShopLoader();
        try {
            shopLoader.setUrl(new URL("http://file.nidama.net/class/mobile_develop/data/bookstore.json"));
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }


        Handler handler = new Handler(Looper.getMainLooper()){
            @Override
            public void handleMessage(@NonNull Message msg) {
                super.handleMessage(msg);
                List<LatLng> shopPoint;
                List<String> shopName;
                if(msg.what==0) {
                    String content = msg.getData().getString("data");
                    shopPoint = shopLoader.parsonJson(content);
                    shopName = shopLoader.getShopName(content);
                    for(int index=0;index<shopPoint.size();index++){
                        BitmapDescriptor bitmap = BitmapDescriptorFactory.fromResource(R.drawable.diana);
                        OverlayOptions markerOption = new MarkerOptions().icon(bitmap).position(shopPoint.get(index));
                        OverlayOptions textOption = new TextOptions().bgColor(0xAAFFFF00).fontSize(50).
                                fontColor(0xFFFF00FF).text(shopName.get(index)).rotate(0).position(shopPoint.get(index));
                        baiduMap.addOverlay(textOption);
                        baiduMap.addOverlay(markerOption);
                    }
                }
            }
        };
        Runnable runnable = () -> {
            try {
                Message message = shopLoader.download();
                handler.sendMessage(message);
            } catch (IOException e) {
                e.printStackTrace();
            }
        };
        new Thread(runnable).start();




        baiduMap.setOnMarkerClickListener(arg0 -> {
            Toast.makeText(getContext(), "Marker被点击了！", Toast.LENGTH_SHORT).show();
            return false;
        });

        return rootView;
    }

    @Override
    public void onPause() {
        super.onPause();
        //在activity执行onPause时执行mMapView. onPause ()，实现地图生命周期管理
        mapView.onPause();
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        //在activity执行onDestroy时执行mMapView.onDestroy()，实现地图生命周期管理
        mapView.onDestroy();
    }

}